/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.q1cc.cfs.jamp;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragGestureRecognizer;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetContext;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;
import javax.swing.JWindow;

public class TestGrafik extends JWindow implements DropTargetListener, DragSourceListener,DragGestureListener
{
    public JTextArea ta = new JTextArea();;
    private String file = null;
    public String getFile(){return file;}
    public JWindow jw = this;
    public void setFile(String file){this.file = file;}
    public TestGrafik()
    {
        setSize(300,300);
        getContentPane().add(ta, BorderLayout.CENTER);
        DropTarget dt = new DropTarget(ta, this);
        DragSource ds = new DragSource();
        ds.addDragSourceListener(this);
        DragGestureRecognizer dgr=ds.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY, this);
        setVisible(true);
    }
    public void dragEnter(DropTargetDragEvent dtde) {}
    public void dragExit(DropTargetEvent dte) {}
    public void dragOver(DropTargetDragEvent dtde) {
        dtde.acceptDrag(DnDConstants.ACTION_COPY);
        Transferable t= dtde.getTransferable();
        DataFlavor[] df=t.getTransferDataFlavors();
        for(DataFlavor dt:df)
        {
            System.out.println(dt.getHumanPresentableName());
        }

        try {
            t.getTransferData(null);
        } catch (UnsupportedFlavorException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    public void dropActionChanged(DropTargetDragEvent dtde) {}
    public void drop(DropTargetDropEvent dtde)
    {
        try
        {
            Transferable tr = dtde.getTransferable();
            DataFlavor[] flavors = tr.getTransferDataFlavors();
            for (int i = 0; i < flavors.length; i++)
            {

                if (flavors[i].isFlavorJavaFileListType())
                {
                    dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                    setFile(((java.util.List)tr.getTransferData(flavors[i])).get(0).toString());
                    System.out.println(file);
                    dtde.dropComplete(true);
                    return;
                }
                else if (flavors[i].isRepresentationClassInputStream())
                {
                    dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                    ta.read(new InputStreamReader((InputStream)tr.getTransferData(flavors[i])),"from system clipboard");
                    dtde.dropComplete(true);
                    setFile(ta.getText().trim());
                    ta.setText(null);
                    System.out.println(getFile());
                    return;
                }
            }
            System.out.println("Drop failed: " + dtde);
            dtde.rejectDrop();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            dtde.rejectDrop();
        }
    }
    public static void main(String args[])
    {
        new TestGrafik();
    }

    public void dragEnter(DragSourceDragEvent dsde) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void dragOver(DragSourceDragEvent dsde) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void dropActionChanged(DragSourceDragEvent dsde) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void dragExit(DragSourceEvent dse) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void dragDropEnd(DragSourceDropEvent dsde) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void dragGestureRecognized(DragGestureEvent dge) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}